#include "form.h"
#include "ui_form.h"
#include <QDebug>
#include <QSerialPortInfo>
Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{
    ui->setupUi(this);
    //去窗口边框
    setWindowFlags(Qt::FramelessWindowHint | windowFlags());
    QStringList m_serialPortName;
        foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts()) //获取有效的串口列表
        {
            m_serialPortName << info.portName();
            qWarning()<<"serialPortName:"<<info.portName();
        }
    // 创建串口对象
    serialPort = new QSerialPort(this);

    if(serialPort->isOpen())//判断对象是否已经打开，如果是就关掉
       {
           serialPort->clear();
           serialPort->close();
       }
    // 设置串口名称、波特率等参数
    serialPort->setPortName("COM2"); // 根据实际情况修改串口名称
    serialPort->setBaudRate(QSerialPort::Baud9600);

    // 打开串口
    if (serialPort->open(QIODevice::ReadWrite)) {
        qDebug() << "串口打开成功";
    } else {
        qDebug() << "串口打开失败";
    }
    //把串口的可读信号和槽函数连接
    connect(serialPort,SIGNAL(readyRead()),this,SLOT(slotReadData()));
    /* 图标初始化*/
    insertPixmap(ui->label_7,"g:/QtProjectImages/空调关.png");
    insertPixmap(ui->label_14,"g:/QtProjectImages/空调关.png");
    insertPixmap(ui->label_19,"g:/QtProjectImages/空调关.png");
    insertPixmap(ui->label_10,"g:/QtProjectImages/电视关.png");
    insertPixmap(ui->label_5,"g:/QtProjectImages/窗帘.png");

}

void Form::slotReadData()
{
    QString data = serialPort->readAll();
    qWarning()<<"接收数据："<<data;
    /*厨房灯控制*/
    LampControl(data,"A1","A2",ui->label);
    /* 客厅灯控制*/
    LampControl(data,"B1","B2",ui->label_4);
    /* 书房灯控制*/
    LampControl(data,"C1","C2",ui->label_8);
    /* 主卧灯控制*/
    LampControl(data,"D3","D4",ui->label_20);
    /* 次卧灯控制*/
    LampControl(data,"E1","E2",ui->label_13);
    /* 卫生间灯控制*/
    LampControl(data,"G1","G2",ui->label_16);

    /*客厅空调*/
    airConditioningControl(data,"B5","B6",ui->label_7);
    /*次卧空调*/
    airConditioningControl(data,"E3","E4",ui->label_14);
    /*主卧空调*/
    airConditioningControl(data,"D1","D2",ui->label_19);

    /*客厅电视*/
    TelevisionControl(data,"B7","B8",ui->label_10);
    /*客厅窗帘*/
    CurtainControl(data,"B3","B4",ui->label_5);


}
void Form::insertPixmap( QLabel *label,const QString addr)
{
    /* 插入图标*/
    QPixmap pixmap;
    if(false==pixmap.load(addr))
    {
        qDebug()<<"图片加载失败"<<endl;

    }
    //插入图片
    label->setPixmap(pixmap);
    //缩放图片 适应边框
    label->setScaledContents(true);
}
void Form::LampControl(QString data,QString data1,QString data2,QLabel *label)
{
    if(data==data1)
    {
       insertPixmap(label,"g:/QtProjectImages/灯开.png");
    }
    if(data==data2)
    {
        insertPixmap(label,"g:/QtProjectImages/灯泡.png");
    }
}
void Form::airConditioningControl(QString data,QString data1,QString data2,QLabel *label)
{

    if(data==data1)
    {
       insertPixmap(label,"g:/QtProjectImages/空调.png");
    }
    if(data==data2)
    {
        insertPixmap(label,"g:/QtProjectImages/空调关.png");
    }

}

void Form::TelevisionControl(QString data,QString data1,QString data2,QLabel *label)
{
    if(data==data1)
    {
       insertPixmap(label,"g:/QtProjectImages/电视.png");
    }
    if(data==data2)
    {
        insertPixmap(label,"g:/QtProjectImages/电视关.png");
    }
}

void Form::CurtainControl(QString data,QString data1,QString data2,QLabel *label)
{
    if(data==data1)
    {
       insertPixmap(label,"g:/QtProjectImages/窗帘关.jpg");
    }
    if(data==data2)
    {
        insertPixmap(label,"g:/QtProjectImages/窗帘.png");
    }
}

void Form::mouseMoveEvent(QMouseEvent *e)
{
    if (e->buttons() & Qt::LeftButton) {
               this->move(this->x() + e->globalX() - p.x(), this->y() + e->globalY() - p.y());
               p = e->globalPos();
           }
}

void Form::mousePressEvent(QMouseEvent *e)
{
    if (e->button() == Qt::LeftButton) {
               p = e->globalPos();
    }
}
Form::~Form()
{
    delete ui;
}

void Form::on_toolButton_clicked()
{
    showMinimized();
}

void Form::on_toolButton_2_clicked()
{

}

void Form::on_toolButton_3_clicked()
{
    close();
}
